<?php
require_once('Common.php');
class Example1 extends Common
{

	function displayHtml()
	{
		?>
	
			<br />

			<div align="left" style="position: relative;"><div id="ajaxLoader1"><img src="images/ajax_loader.gif" alt="Loading..." /></div></div>
			
			<br />
			
			<div id="historyButtonsLayer" align="left">
			</div>
	
			<div id="historyContainer">
				<div id="information">
				</div>
		
				<div id="titleLayer" style="padding: 2px; font-weight: bold; font-size: 18px; text-align: center;">
				</div>
		
				<div id="tableLayer" align="center">
				</div>
				
				<div id="recordLayer" align="center">
				</div>		
				
				<div id="searchButtonsLayer" align="center">
				</div>
			</div>

			<br /><br /><br /><br /><br /><br /><br />

			<script type="text/javascript">
				trackHistory = false;
				var ajaxUrl = '<?php echo $_SERVER['PHP_SELF']; ?>';
				toAjaxTableEditor('update_html','');
			</script>
		<?php
	}

	function initiateEditor()
	{
		$tableColumns['course_title'] = array('display_text' => 'Title', 'perms' => 'ETVAXQS');
		$tableColumns['course_number'] = array('display_text' => 'Name', 'perms' => 'EVTAXQS');
		$tableColumns['Credit'] = array('display_text' => 'Credit', 'perms' => 'EVTAXQS');
		$tableColumns['level'] = array('display_text' => 'Level', 'perms' => 'EVTAXQS');
		$tableColumns['prereq'] = array('display_text' => 'Prereq', 'perms' => 'EVTAXQS');
		$tableColumns['OUS'] = array('display_text' => 'OUS', 'perms' => 'EVTAXQS');
		$tableColumns['NCAA'] = array('display_text' => 'NCAA', 'perms' => 'EVTAXQS');
		$tableColumns['course_desc'] = array('display_text' => 'Desc', 'perms' => 'EVTAXQS', 'textarea' => array('rows' => 7, 'cols' => 50));
		$tableColumns['grad_req'] = array('display_text' => 'GradReq', 'perms' => 'EVTAXQS');
		$tableColumns['repeat_credit'] = array('display_text' => 'Rep.Credit', 'perms' => 'EVTAXQS');
		#$tableColumns['id'] = array('display_text' => 'ID', 'perms' => 'EVCTAXQS');
		$tableColumns['lowerboundgrd'] = array('display_text' => 'LowGrade', 'perms' => 'EVTAXQS');
		$tableColumns['upperboundgrd'] = array('display_text' => 'HighGrade', 'perms' => 'EVTAXQS');
		
		
		$tableName = 'courses';
		$primaryCol = 'course_title';
		$errorFun = array(&$this,'logError');
		$permissions = 'EAVIDQCSX';
		
		require_once('php/AjaxTableEditor.php');
		$this->Editor = new AjaxTableEditor($tableName,$primaryCol,$errorFun,$permissions,$tableColumns);
		$this->Editor->setConfig('tableInfo','cellpadding="1" width="1200" class="mateTable"');
		$this->Editor->setConfig('orderByColumn','course_title');
		$this->Editor->setConfig('addRowTitle','Add Course');
		$this->Editor->setConfig('editRowTitle','Edit Course');
		
		$value = "damn";
		$sets = array();
		$whereClause = '';
		// Quick search
		if(!empty($value))
		{
			foreach($this->Editor->tableColumns as $col => $info)
			{
				if(stristr($info['perms'],'Q'))
				{
					if(isset($info['join']) && is_array($info['join']))
					{
						$joinTable = !empty($info['join']['db']) ? $info['join']['db'].'.'.$info['join']['table'] : $info['join']['table'];
						$info['join']['display_mask'] = isset($info['join']['display_mask']) ? $info['join']['display_mask'] : $joinTable.'.'.$info['join']['column'];
						$sets[] = $info['join']['display_mask']." like '%".$this->Editor->escapeData($value)."%'";
					}
					else if(isset($info['display_mask']))
					{
						$sets[] = $info['display_mask']." like '%".$this->Editor->escapeData($value)."%'";
					}
					else
					{
						$sets[] = $this->Editor->dbAndTable.'.`'.$col."` like '%".$this->Editor->escapeData($value)."%'";
					}
				}
			}
		}
		
	}
	
	
	function Example1()
	{
		if(isset($_POST['json']))
		{
			
			session_start();
			$this->mysqlConnect();
			if(ini_get('magic_quotes_gpc'))
			{
				$_POST['json'] = stripslashes($_POST['json']);
			}
			if(function_exists('json_decode'))
			{
				$data = json_decode($_POST['json']);
			}
			else
			{
				require_once('php/JSON.php');
				$js = new Services_JSON();
				$data = $js->decode($_POST['json']);
			}
			if(empty($data->info) && strlen(trim($data->info)) == 0)
			{
				$data->info = '';
			}
			$this->initiateEditor();
			$this->Editor->main($data->action,$data->info);
			if(function_exists('json_encode'))
			{
				echo json_encode($this->Editor->retArr);
			}
			else
			{
				echo $js->encode($this->Editor->retArr);
			}
		}
		else if(isset($_GET['export']))
		{
			session_start();
			ob_start();
			$this->mysqlConnect();
			$this->initiateEditor();
			echo $this->Editor->exportInfo();
			header("Cache-Control: no-cache, must-revalidate");
			header("Pragma: no-cache");
			header("Content-type: application/x-msexcel");			
			header('Content-Type: text/csv');
			header('Content-Disposition: attachment; filename="'.$this->Editor->tableName.'.csv"');
			exit();
		}
		else
		{
			$this->displayHeaderHtml();
			$this->displayHtml();
			$this->displayFooterHtml();
		}
	}
}
$lte = new Example1();
?>